package com.thoughtworks.jsstaticanalysis;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;

import java.io.*;
import java.util.List;

public class JavasrciptSummaryReport {

    private StringTemplateGroup group = new StringTemplateGroup("report");
    private final String enhanceJsFilesFolder = "js";


    public void report(JavascriptSummary summary, String reportFile) throws IOException {
        new File(enhanceJsFilesFolder).mkdir();

        StringTemplate page = group.getInstanceOf("page");
        page.setAttribute("entrance", summary.getEntrance());
        setFilesProperty(page, summary);
        wirteReportFile(page, reportFile);
    }

    private void wirteReportFile(StringTemplate page, String reportFile) throws IOException {
        FileWriter writer = new FileWriter(reportFile);
        writer.write(page.toString());
        writer.flush();
        writer.close();
    }

    private void setFilesProperty(StringTemplate page, JavascriptSummary summary) throws IOException {
        for (JavascriptFileSummary fileSummary : summary.getAllFileSummaries()) {
            createEnhancedJS(fileSummary);
            page.setAttribute("files", getFilesSummary(group, fileSummary));
        }
    }

    private void createEnhancedJS(JavascriptFileSummary fileSummary) throws IOException {
        BufferedReader input = new BufferedReader(new FileReader(fileSummary.getJsFile()));
        StringTemplate enhancedJS = group.getInstanceOf("enhancedJS");
        setEnhancedJSLinesProperty(input, enhancedJS);

        BufferedWriter output = new BufferedWriter(new FileWriter(enhanceJsFilesFolder + "/" + fileSummary.getFileName() + ".html"));
        output.write(enhancedJS.toString());
        output.flush();
        output.close();
    }

    private void setEnhancedJSLinesProperty(BufferedReader input, StringTemplate enhancedJS) throws IOException {
        String oneLine = null;
        int index = 1;
        while ((oneLine = input.readLine()) != null) {
            enhancedJS.setAttribute("enhancedJSLines", getEnhancedJSLine(index++, oneLine));
        }
    }

    private StringTemplate getEnhancedJSLine(int index, String line) {
        StringTemplate enhancedJSLine = group.getInstanceOf("enhancedJSLine");
        enhancedJSLine.setAttribute("startLine", index);
        enhancedJSLine.setAttribute("lineContent", Escape.escape(line));
        return enhancedJSLine;
    }

    private StringTemplate getFilesSummary(StringTemplateGroup group, JavascriptFileSummary fileSummary) {
        StringTemplate file = group.getInstanceOf("file");
        file.setAttribute("folderName", fileSummary.getFolderName());
        file.setAttribute("fileName", fileSummary.getFileName());
        setFunctionsProperty(group, file, fileSummary.getFunctions());
        return file;
    }

    private void setFunctionsProperty(StringTemplateGroup group, StringTemplate file, List<FoundFunction> functions) {
        for (FoundFunction func : functions) {
            StringTemplate function = group.getInstanceOf("function");
            function.setAttribute("category", func.maybeConstructor ? "constructor" : (func.lineCount > 10 ? "too-big" : "health"));
            function.setAttribute("functionName", func.funcName);
            function.setAttribute("score", func.score);
            function.setAttribute("lineCount", func.lineCount);
            function.setAttribute("startLine", func.startLine);
            function.setAttribute("endLine", func.endLine);
            function.setAttribute("fileUrl", "js/" + func.fileName + ".html#" + func.startLine);
            file.setAttribute("functions", function);
        }
    }

}
